package Day012_多线程2.demo03_并发包;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhangyan
 * @date 2020/4/30 11:10
 */
public class demo03_ConcurrentHashMap {
    public static void main(String[] args) throws InterruptedException {

        //创建任务
        MyRunnable2 mr2 = new MyRunnable2();
        //创建线程
        Thread t1 = new Thread(mr2);
        t1.start();

        Thread.sleep(2000);

        for (int i = 10001; i <= 20000; i++) {
            mr2.map.put(i, i);
        }
        System.out.println("最后集合键值对的个数:" + mr2.map.size());
        System.out.println(mr2.map);
    }
}
/*
    键值对个数应该是1000个,但实际结果可能会大于1000 (键唯一)
*/

class MyRunnable2 implements Runnable {

    //HashMap<Integer,Integer> map = new HashMap<>();

    //Hashtable<Integer,Integer> map = new Hashtable<>();

    ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            map.put(i, i);
        }
        System.out.println("添加完毕");
    }
}